import openpyxl
from utils.decoractors import group_sort_decorator
from config.config import *

@group_sort_decorator
def read_excel(excel_file,sheet_name):
    """
    从excel中读取数据
    :param excel_file: excel文件名
    :param sheet_name:excel表名
    :return:[{},{}]
    """
    workbook = openpyxl.load_workbook(excel_file)

    # 选择表
    worksheet = workbook[sheet_name]

    data = []
    keys = [cell.value for cell in worksheet[2]] # 拿表的第二行数据，也就是key
    for row in worksheet.iter_rows(min_row=3,values_only=True):
        dict_data = dict(zip(keys,row))
        # 如果读取的is_true字段值是TRUE，则append
        # if dict_data["is_true"]:
        data.append(dict_data)

    workbook.close()
    return data

# def group_sort(data):
#
#     grouped_data = defaultdict(list)
#
#     # 1、遍历了excel读取出来的原始数据，然后进行了分组
#     for story in data:
#         grouped_data[story['story']].append(story)
#
#     # 2、按照order值对场景内的接口顺序进行排序
#     # sorted_grouped_data = {}
#     # for story,item in grouped_data.items():
#     #     sorted_grouped_data[story] = sorted(item,key=itemgetter("order"))
#
#     # 字典推导式，等价于上面三行代码
#     sorted_grouped_data = {story:sorted(item,key=itemgetter("order")) for story,item in grouped_data.items()}
#
#     # 3、把数据结构还原为原始的read_excel()返回的数据结构
#     res_data=[]
#     for value in sorted_grouped_data.values():
#         res_data.extend(value)
#
#     return res_data



if __name__ == '__main__':
    data = read_excel(EXECEL_FILE, SHEET_NAME_MANAGEMENT)
    print(data)

